<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="app-container">
          <avue-tree
            :option="treeOption"
            :data="treeData"
            @node-click="nodeClick"
            :key="treeKey"
          />
        </div>
      </el-col>

      <el-col :span="19">
        <avue-crud
          :option="option"
          :search.sync="search"
          :table-loading="loading"
          :data="data"
          ref="crud"
          v-model="form"
          @row-del="rowDel"
          @row-update="rowUpdate"
          @row-save="rowSave"
          :before-open="beforeOpen"
          :page.sync="page"
          @search-change="searchChange"
          @search-reset="searchReset"
          @selection-change="selectionChange"
          @current-change="currentChange"
          @size-change="sizeChange"
          @refresh-change="refreshChange"
          @on-load="onLoad"
        >
          <template slot="menuLeft">
            <el-button type="danger" icon="el-icon-delete" plain
              >删除</el-button
            >
            <el-button type="info" icon="el-icon-user" plain
              >角色配置</el-button
            >
            <el-button type="primary" icon="el-icon-refresh" plain
              >密码重置</el-button
            >
            <el-button type="warning" plain icon="el-icon-coordinate"
              >账号解封
            </el-button>

            <el-button
              type="success"
              plain
              icon="el-icon-upload2"
              @click="handleImport"
              >导入
            </el-button>
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              @click="handleExport"
              >导出
            </el-button>
          </template>
          <template slot="menu" slot-scope="{ row }">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="btnadd('edit', row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-view"
              @click="btnadd('view', row)"
              >查看</el-button
            >
          </template>
        </avue-crud>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //----------左侧树形------开始-------
      treeId: "",
      baseLevel: "",
      left_obj: {},
      treeKey: 1,
      treeData: [
        {
          value: 0,
          label: "大聪明学府",
          children: [
            {
              value: 1,
              label: "桂林总公司",
            },
            {
              value: 1,
              label: "南宁分公司",
            },
          ],
        },
      ],
      treeOption: {
        defaultExpandAll: true,
        formOption: {
          labelWidth: 100,
          column: [
            {
              label: "自定义项",
              prop: "test",
            },
          ],
        },
      },
      //----------左侧树形------结束-------

      form: {},
      search: {},
      excelBox: false,
      selectionList: [],
      query: {},
      loading: true,
      platformLoading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        indexLabel: "序号",
        calcHeight: 30,
        menuPosition: "center",
        tip: false,
        searchMenuSpan: 6,
        searchLabelWidth: 100,
        border: false,
        index: true,
        selection: true,
        addBtn: true,
        editBtn: true,
        delBtn: true,
        viewBtn: true,
        dialogClickModal: false,
        column: [
          {
            label: "登陆账号",
            prop: "buildingName",
            display: false,
          },
          {
            label: "用户昵称",
            prop: "buildingName",
            display: false,
          },
          {
            label: "所属角色",
            prop: "buildingName",
            display: false,
          },
          {
            label: "所属部门",
            prop: "buildingName",
            display: false,
          },
          {
            label: "最近登录时间",
            prop: "buildingName",
            display: false,
          },
          {
            label: "登陆次数",
            prop: "buildingName",
            display: false,
          },
        ],
        group: [
          {
            label: "基础信息",
            prop: "a170615386127798454",
            arrow: false,
            collapse: true,
            display: true,
            column: [
              {
                type: "input",
                label: "登录账号",
                span: 24,
                display: true,
                prop: "a170615387827357225",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "登录账号必须填写",
                  },
                ],
              },
              {
                type: "password",
                label: "登录密码",
                span: 12,
                display: true,
                prop: "a170615391549229162",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "密码必须填写",
                  },
                ],
              },
              {
                type: "password",
                label: "确认密码",
                span: 12,
                display: true,
                prop: "a170615391717734956",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "确认密码必须填写",
                  },
                ],
              },
            ],
          },
          {
            label: "详细信息",
            prop: "a170615392905752706",
            arrow: false,
            collapse: true,
            display: true,
            column: [
              {
                type: "input",
                label: "用户昵称",
                span: 12,
                display: true,
                prop: "a170615395966091669",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "用户昵称必须填写",
                  },
                ],
              },
              {
                type: "input",
                label: "用户姓名",
                span: 12,
                display: true,
                prop: "a170615396105310249",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "用户姓名必须填写",
                  },
                ],
              },
              {
                type: "input",
                label: "手机号码",
                span: 12,
                display: true,
                prop: "a170615396240579527",
              },
              {
                type: "input",
                label: "电子邮箱",
                span: 12,
                display: true,
                prop: "a170615396578293158",
              },
              {
                type: "select",
                label: "用户性别",
                dicData: [
                  {
                    label: "选项一",
                    value: "0",
                  },
                  {
                    label: "选项二",
                    value: "1",
                  },
                  {
                    label: "选项三",
                    value: "2",
                  },
                ],
                cascader: [],
                span: 12,
                display: true,
                props: {
                  label: "label",
                  value: "value",
                  desc: "desc",
                },
                prop: "a170615397966910059",
              },
              {
                type: "date",
                label: "用户生日",
                span: 12,
                display: true,
                format: "YYYY-MM-DD",
                valueFormat: "YYYY-MM-DD",
                prop: "a170615398841899303",
              },
            ],
          },
          {
            label: "职业信息",
            prop: "a170615414627512361",
            arrow: false,
            collapse: true,
            display: true,
            column: [
              {
                type: "input",
                label: "用户编号",
                span: 12,
                display: true,
                prop: "a170615417503678737",
              },
              {
                type: "tree",
                label: "所属角色",
                span: 12,
                display: true,
                dicData: [
                  {
                    label: "选项一",
                    value: "0",
                    children: [
                      {
                        label: "选项1-1",
                        value: "11",
                      },
                      {
                        label: "选项1-2",
                        value: "12",
                      },
                    ],
                  },
                  {
                    label: "选项二",
                    value: "1",
                  },
                  {
                    label: "选项三",
                    value: "2",
                  },
                ],
                parent: true,
                cascader: [],
                props: {
                  label: "label",
                  value: "value",
                  children: "children",
                  desc: "desc",
                },
                prop: "a170615418123456543",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "所属角色必须填写",
                  },
                ],
              },
              {
                type: "tree",
                label: "所属部门",
                span: 12,
                display: true,
                dicData: [
                  {
                    label: "选项一",
                    value: "0",
                    children: [
                      {
                        label: "选项1-1",
                        value: "11",
                      },
                      {
                        label: "选项1-2",
                        value: "12",
                      },
                    ],
                  },
                  {
                    label: "选项二",
                    value: "1",
                  },
                  {
                    label: "选项三",
                    value: "2",
                  },
                ],
                parent: true,
                cascader: [],
                props: {
                  label: "label",
                  value: "value",
                  children: "children",
                  desc: "desc",
                },
                prop: "a170615418246730350",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "所属部门必须填写",
                  },
                ],
              },
              {
                type: "tree",
                label: "所属岗位",
                span: 12,
                display: true,
                dicData: [
                  {
                    label: "选项一",
                    value: "0",
                    children: [
                      {
                        label: "选项1-1",
                        value: "11",
                      },
                      {
                        label: "选项1-2",
                        value: "12",
                      },
                    ],
                  },
                  {
                    label: "选项二",
                    value: "1",
                  },
                  {
                    label: "选项三",
                    value: "2",
                  },
                ],
                parent: true,
                cascader: [],
                props: {
                  label: "label",
                  value: "value",
                  children: "children",
                  desc: "desc",
                },
                prop: "a17061541834505758",
                required: true,
                rules: [
                  {
                    required: true,
                    message: "所属岗位必须填写",
                  },
                ],
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  methods: {
    nodeClick(data) {
      this.left_obj = data;
      this.treeId = data.id;
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    rowSave(row, done, loading) {
      updateTemp(this.fil_row(row)).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      updateTemp(this.fil_row(row)).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    fil_row(row) {
      row.status = 2;
      return row;
    },

    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return removeLogicRoom(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.treeNursingProjectCateId = "";
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return removeLogicRoom(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },

    beforeOpen(done, type) {
      this.form.parentName = this.left_obj.title;
      if (["edit", "view"].includes(type)) {
        getdetailTemp(this.form.id).then((res) => {
          const data = res.data;
          this.form = data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      //   this.loading = true;
      //   if (this.treeId) {
      //     params.ancestors = this.treeId;
      //   }
      //   getPageList(
      //     page.currentPage,
      //     page.pageSize,
      //     Object.assign(params, this.query),
      //     this.treeNursingProjectCateId
      //   ).then(res => {
      //     const data = res.data.data;
      //     this.page.total = data.total;
      //     this.data = data.records;
      this.loading = false;
      //     this.selectionClear();
      //   });
    },
    btnadd(ntype, row) {},

    handleImport() {
      //   this.$refs.householdexcelref.init();
    },
    callback_fun_add() {
      this.onLoad(this.page);
      this.treeKey++;
    },
    btndel() {
      if (!this.left_obj.id) {
        this.$message.error("请选择左侧要删除的节点");
        return;
      }
      this.$confirm(
        "确定将【" +
          this.left_obj.name +
          "】数据删除?同时也将删除子节点的数据，不可恢复，请慎重！",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          return removeBuildingUnit(this.left_obj.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    callback_fun_excel() {
      this.onLoad(this.page);
      this.treeKey++;
    },
    handleExport() {},
  },
};
</script>

<style lang="scss" scoped>
.avue-tree ::v-deep {
  .avue-tree__content {
    padding: 0;
  }
}
</style>